//
//  HelpViewController.m
//  PurchasingMeeting
//
//  Created by yanseng.lin on 8/15/13.
//  Copyright (c) 2013 YX. All rights reserved.
//

#import "HelpViewController.h"
#import "UserObject.h"
#import "WebServiceManager.h"
#import "DataManager.h"
#import "TipView.h"
#import "PMGlobal.h"
#import "SDImageView+SDWebCache.h"

@interface HelpViewController ()

@end

@implementation HelpViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setNavTitle:@"用户服务"];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showtip:) name:@"showtip" object:nil];
    self.navigationController.navigationBarHidden = NO;
	// Do any additional setup after loading the view.
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
    {
        self.automaticallyAdjustsScrollViewInsets=NO;
    }
    UIImage *img = [[PMGlobal getInstance]getretimage];
    UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
    [b setImage:img forState:UIControlStateNormal];
    b.frame = CGRectMake(10, 10, img.size.width, img.size.height);
    [b addTarget:self action:@selector(ret) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithCustomView:b];
    self.navigationItem.leftBarButtonItem = left;

    
    UIImage *bgImage = [[UIHelper getInstance] getBundleImage:@"suit_bg.jpg"];
    self.view.backgroundColor = [UIColor colorWithPatternImage:bgImage];
//    UIImage*backimg = [[UIHelper getInstance] getBundleImage:@"suit_bg.jpg"];
//    UIImageView *back = [[UIImageView alloc]initWithImage:backimg];
//    back.frame = CGRectMake(0, 0, backimg.size.width, backimg.size.height);
//    [self.view addSubview:back];
#ifdef DHH_iOS
    img = [[UIHelper getInstance] getBundleImage:@"iphone_tint1.png"];
    tint = [[UIImageView alloc]initWithImage:img];
    tint.frame = CGRectMake(0, 0, img.size.width, img.size.height);
    tint.userInteractionEnabled = YES;
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
    {
        tint.top = 64;
    }
    [self.view addSubview:tint];
    
    img = [[UIHelper getInstance] getBundleImage:@"helptintbutton.png"];
    UIButton *notification = [UIButton buttonWithType:UIButtonTypeCustom];
    notification.frame = CGRectMake(200, 7, img.size.width, img.size.height);
    notification.centerX = 64;
    [notification setTitle:@"重要通知" forState:UIControlStateNormal];
    notification.titleLabel.font = [UIFont systemFontOfSize:14];
    [notification setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [notification addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
    notification.tag = 1;
    [tint addSubview:notification];
    
    UIButton *conferenceday = [UIButton buttonWithType:UIButtonTypeCustom];
    conferenceday.frame = CGRectMake(50, 7, img.size.width, img.size.height);
    conferenceday.centerX = 128;
    [conferenceday setTitle:@"展厅平面图" forState:UIControlStateNormal];
    conferenceday.titleLabel.font = [UIFont systemFontOfSize:14];
    [conferenceday setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [conferenceday addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
    conferenceday.tag = 2;
    [tint addSubview:conferenceday];
    
    UIButton *receive = [UIButton buttonWithType:UIButtonTypeCustom];
    receive.frame = CGRectMake(150, 7, img.size.width, img.size.height);
    receive.centerX = 192;
    [receive setTitle:@"会议日程" forState:UIControlStateNormal];
    receive.titleLabel.font = [UIFont systemFontOfSize:14];
    [receive setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [receive addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
    receive.tag = 3;
    [tint addSubview:receive];
    
    UIButton *order = [UIButton buttonWithType:UIButtonTypeCustom];
    order.frame = CGRectMake(250, 7, img.size.width, img.size.height);
    order.centerX = 256;
    [order setTitle:@"联络信息" forState:UIControlStateNormal];
    order.titleLabel.font = [UIFont systemFontOfSize:14];
    [order setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [order addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
    order.tag = 4;
    [tint addSubview:order];
    
    [conferenceday setBackgroundImage:img forState:UIControlStateNormal];
    button = conferenceday;
#else
    UIImage *tintImage = [[UIHelper getInstance] getBundleImage:@"detail_tint.png"];
    tintImage = [tintImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 16, 0, 16)];
    
    tint = [[UIImageView alloc]initWithImage:tintImage];
    tint.width = 1024;
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
    {
        tint.top = 64;
    }
    tint.userInteractionEnabled = YES;
    [self.view addSubview:tint];
    
    img = [[UIHelper getInstance] getBundleImage:@"helptintbutton.png"];
    UIButton *notification = [UIButton buttonWithType:UIButtonTypeCustom];
    notification.frame = CGRectMake(200, 7, img.size.width, img.size.height);
    notification.center = CGPointMake(212, tint.height/2);
    [notification setTitle:@"重要通知" forState:UIControlStateNormal];
    notification.titleLabel.font = [UIFont systemFontOfSize:14];
    [notification setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [notification addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
    notification.tag = 1;
    [tint addSubview:notification];
    
    UIButton *conferenceday = [UIButton buttonWithType:UIButtonTypeCustom];
    conferenceday.frame = CGRectMake(400, 7, img.size.width, img.size.height);
    conferenceday.center = CGPointMake(412, tint.height/2);
    [conferenceday setTitle:@"会议须知" forState:UIControlStateNormal];
    conferenceday.titleLabel.font = [UIFont systemFontOfSize:14];
    [conferenceday setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [conferenceday addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
    conferenceday.tag = 2;
    [tint addSubview:conferenceday];
    
    UIButton *receive = [UIButton buttonWithType:UIButtonTypeCustom];
    receive.frame = CGRectMake(600, 7, img.size.width, img.size.height);
    receive.center = CGPointMake(612, tint.height/2);
    [receive setTitle:@"会议日程" forState:UIControlStateNormal];
    receive.titleLabel.font = [UIFont systemFontOfSize:14];
    [receive setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [receive addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
    receive.tag = 3;
    [tint addSubview:receive];
    
    UIButton *order = [UIButton buttonWithType:UIButtonTypeCustom];
    order.frame = CGRectMake(800, 7, img.size.width, img.size.height);
    order.center = CGPointMake(812, tint.height/2);
    [order setTitle:@"联络信息" forState:UIControlStateNormal];
    order.titleLabel.font = [UIFont systemFontOfSize:14];
    [order setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [order addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
    order.tag = 4;
    [tint addSubview:order];
    
    [conferenceday setBackgroundImage:img forState:UIControlStateNormal];
    button = conferenceday;
#endif
    view = [[UIView alloc]initWithFrame:CGRectMake(0, 44, 1024, 724)];
    view.top = tint.bottom;
    view.userInteractionEnabled = YES;
    [self.view addSubview:view];
    
    img = [[UIHelper getInstance] getBundleImage:@"help_image.png"];
    UIImageView *imageview = [[UIImageView alloc]initWithImage:img];
    imageview.userInteractionEnabled = YES;
    imageview.frame = CGRectMake(0, 0, img.size.width, img.size.height);
    imageview.center = CGPointMake(512, 306);
    [view addSubview:imageview];
    
    UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 900, 550)];
    scroll.clipsToBounds = YES;
    //scroll.center = CGPointMake(imageview.width/2, 300);
    [imageview addSubview:scroll];
    scroll.center = CGPointMake( imageview.frame.size.width/2,imageview.frame.size.height/2);
    scroll.userInteractionEnabled=YES;
    
    UIImage *img1 = [[UIHelper getInstance] getBundleImage:@"help-1.jpg"];
    UIImageView *imageview1 = [[UIImageView alloc]initWithImage:img1];
    imageview1.frame = CGRectMake(0, 0, scroll.width/2, scroll.height/2);
    imageview1.center = CGPointMake(scroll.width/2, scroll.height/2);
    
    [imageview1 setImageWithURL:[NSURL URLWithString:[self getUrl:1]] block:^(UIImageView *view, UIImage *image) {
        scroll.contentSize = CGSizeMake(image.size.width/2, image.size.height/2);
        view.frame = CGRectMake(0, 0, image.size.width/2, image.size.height/2);
        view.center = CGPointMake(scroll.width/2, scroll.height/2);
        view.top = 10;
    }];
    [scroll addSubview:imageview1];
    
}

- (NSString *)getUrl:(NSInteger)index{
    NSDictionary *d = nil;
    NSArray *arr = [[PMGlobal getInstance] getImageConfig];
    for (NSDictionary *dic in arr) {
        if ([[dic objectForKey:@"PicCode"] isEqualToString:[NSString stringWithFormat:@"UserCenter%d",(long)index]]) {
            d = dic;
            break;
        }
    }
    NSString *url = [d objectForKey:@"url"];
    NSString *path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:url];
    return path;
}

-(void)ret
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)clicked:(UIButton *)sender
{
    [view removeFromSuperview];
    UIButton *btn = sender;
    [button setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"clear.png"] forState:UIControlStateNormal];
    button = btn;
    [btn setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"helptintbutton.png"] forState:UIControlStateNormal];
#ifdef DHH_iOS
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
    {
        view = [[UIView alloc]initWithFrame:CGRectMake(0, 44+64, 320, self.view.height-44)];
    }
    else
    {
        view = [[UIView alloc]initWithFrame:CGRectMake(0, 44, 320, self.view.height-44)];
    }
    [self.view addSubview:view];

    UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 0, 300, 320)];
    scroll.userInteractionEnabled =YES;
    //scroll.clipsToBounds = YES;
    //scroll.center = CGPointMake(imageview.width/2, 100);
    [view addSubview:scroll];
    scroll.centerX = view.frame.size.width/2;
    if (sender.tag == 1) {
        scroll.top = 50;
        
        UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 200, 40)];
        title.font = [UIFont systemFontOfSize:25];
        title.text = @"订货会现场通知：";
        title.backgroundColor = [UIColor clearColor];
        title.center = CGPointMake(160, 20);
        [view addSubview:title];
        
        NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
        NSDate *date = [formatter dateFromString:@"1900-01-01 00:00:00"];
        NSString *time = [formatter stringFromDate:date];
        UserObject *userobj = [[DataManager sharedManager]getUserObject];
        [[WebServiceManager sharedManager ] gettip:userobj.userCode time:time completion:^(NSArray *tips)
         {
             __block int bottom=0;
             for (int i = 0; i<tips.count; i++) {
                 NSDictionary *dic = [tips objectAtIndex:i];
                 NSString *str = [dic objectForKey:@"content"];
                 UIFont *f = [UIFont systemFontOfSize:17];
                 NSArray *breakNum = [[str stringByReplacingOccurrencesOfString:@"\r\n" withString:@"\n"] componentsSeparatedByString:@"\n"];
                 
                 int totalNum = 0;
                 for (NSString *subS in breakNum) {
                     CGSize s = [subS sizeWithFont:f];
                     int num = ceil(s.width/600.0);
                     if (num==0) {
                         num=1;
                     }
                     totalNum += num;
                 }
                 
                 int titleHeight = 30;
                 
                 str = [str stringByReplacingOccurrencesOfString:@"\r\n" withString:@"\n"];
                 
                 CGSize s = [str sizeWithFont:f];
                 float height = s.height * (totalNum);
                 UIView *tipview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 800, titleHeight+height)];
                 tipview.top = bottom;
                 bottom=tipview.bottom;
                 [scroll addSubview:tipview];
                 
                 UIImage *img = [[UIHelper getInstance] getBundleImage:@"latter.png"];
                 UIImageView *latterview = [[UIImageView alloc]initWithImage:img];
                 latterview.frame = CGRectMake(10, 5, img.size.width, img.size.height);
                 [tipview addSubview:latterview];
                 
                 UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(40, 0, 500, titleHeight)];
                 title.text = [dic objectForKey:@"title"];
                 [tipview addSubview:title];
                 title.font = [UIFont boldSystemFontOfSize:20];
                 
                 UILabel *content = [[UILabel alloc]initWithFrame:CGRectMake(40, titleHeight+5, 700, 0)];
                 content.text = str;
                 content.font = f;
                 content.height = tipview.height - titleHeight ;
                 content.numberOfLines=0;
                 [tipview addSubview:content];
                 
                 UILabel *time = [[UILabel alloc]initWithFrame:CGRectMake(600, 0, 200, titleHeight)];
                 time.text = [dic objectForKey:@"d_id"];
                 time.font = [UIFont boldSystemFontOfSize:20];
                 [tipview addSubview:time];
                 scroll.contentSize = CGSizeMake(800, bottom);
                 
                 bottom+=10;
             }
         }];
    }

#else
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
    {
        view = [[UIView alloc]initWithFrame:CGRectMake(0, 44+64, 1024, 724)];
    }
    else
    {
        view = [[UIView alloc]initWithFrame:CGRectMake(0, 44, 1024, 724)];
    }
    [self.view addSubview:view];
    
    UIImage *img = [[UIHelper getInstance] getBundleImage:@"help_image.png"];
    UIImageView *imageview = [[UIImageView alloc]initWithImage:img];
    imageview.userInteractionEnabled = YES;
    imageview.frame = CGRectMake(0, 0, img.size.width, img.size.height);
    imageview.center = CGPointMake(512, 306);
    [view addSubview:imageview];
    
    
    UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, img.size.width, 530)];
    scroll.userInteractionEnabled =YES;
    //scroll.clipsToBounds = YES;
    //scroll.center = CGPointMake(imageview.width/2, 100);
    [imageview addSubview:scroll];
    scroll.center = CGPointMake( imageview.frame.size.width/2,imageview.frame.size.height/2+30);
    scroll.clipsToBounds = YES;
    UIImage *img1;
    if (sender.tag == 1) {
        scroll.top = 150;
        UIImage *img = [[UIHelper getInstance] getBundleImage:@"suit_welcome.png"];
        UIImageView *welcome = [[UIImageView alloc]initWithImage:img];
        welcome.frame = CGRectMake(0, 0, img.size.width, img.size.height);
        welcome.center = CGPointMake(imageview.width/2,70);
        [imageview addSubview:welcome];
        
        UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 200, 40)];
        title.font = [UIFont systemFontOfSize:25];
        title.text = @"订货会现场通知：";
        [imageview addSubview:title];
        
        NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
        NSDate *date = [formatter dateFromString:@"1900-01-01 00:00:00"];
        NSString *time = [formatter stringFromDate:date];
        UserObject *userobj = [[DataManager sharedManager]getUserObject];
        [[WebServiceManager sharedManager ] gettip:userobj.userCode time:time completion:^(NSArray *tips)
         {
             __block int bottom=0;
             for (int i = 0; i<tips.count; i++) {
                 NSDictionary *dic = [tips objectAtIndex:i];
                 NSString *str = [dic objectForKey:@"content"];
                 UIFont *f = [UIFont systemFontOfSize:17];
                 NSArray *breakNum = [[str stringByReplacingOccurrencesOfString:@"\r\n" withString:@"\n"] componentsSeparatedByString:@"\n"];
                 
                 int totalNum = 0;
                 for (NSString *subS in breakNum) {
                     CGSize s = [subS sizeWithFont:f];
                     int num = ceil(s.width/600.0);
                     if (num==0) {
                         num=1;
                     }
                     totalNum += num;
                 }
                 
                 int titleHeight = 30;
 
                 str = [str stringByReplacingOccurrencesOfString:@"\r\n" withString:@"\n"];
                 
                 CGSize s = [str sizeWithFont:f];
                 float height = s.height * (totalNum);
                 UIView *tipview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 800, titleHeight+height)];
                 tipview.top = bottom;
                 bottom=tipview.bottom;
                 [scroll addSubview:tipview];
                 
                 UIImage *img = [[UIHelper getInstance] getBundleImage:@"latter.png"];
                 UIImageView *latterview = [[UIImageView alloc]initWithImage:img];
                 latterview.frame = CGRectMake(10, 5, img.size.width, img.size.height);
                 [tipview addSubview:latterview];

                 UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(40, 0, 500, titleHeight)];
                 title.text = [dic objectForKey:@"title"];
                 [tipview addSubview:title];
                 title.font = [UIFont boldSystemFontOfSize:20];
                 
                 UILabel *content = [[UILabel alloc]initWithFrame:CGRectMake(40, titleHeight+5, 700, 0)];
                 content.text = str;
                 content.font = f;
                 content.height = tipview.height - titleHeight ;
                 content.numberOfLines=0;
                 [tipview addSubview:content];
                 
                 UILabel *time = [[UILabel alloc]initWithFrame:CGRectMake(600, 0, 200, titleHeight)];
                 time.text = [dic objectForKey:@"d_id"];
                 time.font = [UIFont boldSystemFontOfSize:20];
                 [tipview addSubview:time];
                 scroll.contentSize = CGSizeMake(800, bottom);
                 
                 bottom+=10;
             }
         }];
    }
    if (sender.tag == 2) {
        img1 = [[UIHelper getInstance] getBundleImage:@"help-1.jpg"];
    }
    if (sender.tag == 3) {
        img1 = [[UIHelper getInstance] getBundleImage:@"help-2.jpg"];
    }
    if (sender.tag == 4) {
        img1 = [[UIHelper getInstance] getBundleImage:@"help-3.jpg"];
    }
    
    UIImageView *imageview1 = [[UIImageView alloc]initWithImage:img1];
    imageview1.frame = CGRectMake(0, 0, img1.size.width/2, img1.size.height/2);
    if (imageview1.height>scroll.height) {
        imageview1.top = 10;
    }else{
        imageview1.center = CGPointMake(scroll.width/2, scroll.height/2);
    }
    
    [imageview1 setImageWithURL:[NSURL URLWithString:[self getUrl:sender.tag-1]] block:^(UIImageView *view, UIImage *image) {
        scroll.contentSize = CGSizeMake(image.size.width/2, image.size.height/2);
        view.frame = CGRectMake(0, 0, image.size.width/2, image.size.height/2);
        view.center = CGPointMake(scroll.width/2, scroll.height/2);
        view.top = 10;
    }];
    
    [scroll addSubview:imageview1];
//    scroll.contentSize = CGSizeMake(img1.size.width/2, img1.size.height/2);
#endif
}

#pragma mark 消息更新弹出框
-(void)showtip:(NSNotification *)sender
{
    NSArray *tips = [[DataManager sharedManager]getTipsarray];
    if (tips.count==0) {
        return;
    }
    NSDictionary *tipdic = [tips objectAtIndex:tips.count-1];
    
    [self performSelectorOnMainThread:@selector(addTipView:) withObject:tipdic waitUntilDone:YES];
    
}

- (void)addTipView:(NSDictionary *)dic{
    TipView *tipview = [[TipView alloc]initWithdic:dic];
    [self.navigationController.view addSubview:tipview];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
